package com.example.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

@Component
public class ApiErrorCreator {

    @Autowired
    MessageSource messageSource;

    public ApiError createApiError(WebRequest request, String errorCode,
            String defaultErrorMessage, Object... arguments) {

        String localizedMessage = messageSource.getMessage(errorCode, arguments,
                defaultErrorMessage, request.getLocale());

        return new ApiError(errorCode, localizedMessage);
    }
}
